//Author:       Miguel Dominguez & Heath Cazeau
//Date:         3/9/2011
//Class:        Software Engineering
//Project:      Jaz Interpreter
//File:         main.cpp

#include "Instruction.h"
#include "InstructionSet.h"
#include "Lexer.h"
#include "Executor.h"
#include <string>
#include <stack>
#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;
using namespace operations;

int main (int argc, char* argv[])
{
    if(argv[1] != NULL)
    {
        //Prepare for input
        string input_file = argv[1];
        Executor machine_state;
        ifstream stream(input_file.c_str(), ios_base::in);

        //Creates a linked list of Instructions from the input file
        machine_state.mainEnvironment.LinkedList = Lexer::ToInstructions(stream);
        stream.close();

        //Create Map for location of labels
        machine_state.mainEnvironment.LabelLocations = Lexer::GetLabelLocations(machine_state.mainEnvironment.LinkedList);

        //Strips labels from code and adjust links accordingly
        Lexer::StripLabelsFromCode(machine_state.mainEnvironment.LinkedList,machine_state.mainEnvironment.LabelLocations);

        //Execute instructions
        machine_state.ExecuteInstructions();
    }

    else
    {
        cout << "ERROR: Please input a jaz file as a parameter.\n\n";
    }

    return 0;
}
